package com.businesswebsite.businesswebsite.FormalWebExplot.Controller;

import com.alibaba.fastjson.JSONObject;
import com.businesswebsite.businesswebsite.FormalWebExplot.Entity.HotEvent;
import com.businesswebsite.businesswebsite.FormalWebExplot.Service.Event.HotEventService;
import com.businesswebsite.businesswebsite.FormalWebExplot.Utils.Renum;
import com.businesswebsite.businesswebsite.FormalWebExplot.Utils.Result;
import com.businesswebsite.businesswebsite.FormalWebExplot.Utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@CrossOrigin(origins = "http://47.108.80.35:8080" , allowCredentials = "true")
//@CrossOrigin(origins = "http://localhost:8080" , allowCredentials = "true")
@RestController
@RequestMapping("hot_event")
public class HotEventController {
    @Autowired
    private HotEventService hotEventService;
    /*
    * 获取所有事件
    * */
    @GetMapping("/get_event")
    public Result getHotEvent(){
        try{
            List<HotEvent> hotEvent = hotEventService.getHotEvent();
            if(hotEvent.size() >0 ) {
                return ResultUtil.success(hotEvent);
            }else{
                return ResultUtil.error(Renum.DATA_IS_NULL.getCode(),"数据为空");
            }
        }catch (Exception e){
            e.printStackTrace();
            return  ResultUtil.error(Renum.UNKNOWN_ERROR.getCode(),e.getMessage());
        }
    }

    /*
     *新增事件
     *  */
    @PostMapping("/add_event")
    public Result addHotEvent(@RequestBody Map<String,Object> hotEvent){
        String id = UUID.randomUUID().toString().replace("-","");
        hotEvent.put("event_id",id);
        try{
            if(id != null && hotEvent.get("event_name") != null && hotEvent.get("event_content") != null && hotEvent.get("event_time") != null){
                return ResultUtil.error(Renum.DATA_IS_NULL.getCode(),"数据为空");
            }else{
                Integer res = hotEventService.addHotEvent(hotEvent);
                return ResultUtil.success("新增数据成功"+"\n\n 新增"+res+"条数据"+ "\n\n"+JSONObject.toJSON(hotEvent));
            }
        }catch (Exception e ){
            return ResultUtil.error(Renum.UNKNOWN_ERROR.getCode(),Renum.UNKNOWN_ERROR.getMsg());
        }
    }

    /*
    * 删除事件 管理员权限
    * */
    @DeleteMapping("/delete_event/{id}")
    public Result delHotEvent(@PathVariable String id){
        try{
            if(id != null){
                Boolean res = hotEventService.delHotEvent(id);
                if (res){
                    return ResultUtil.success("删除成功");
                }else{
                    return ResultUtil.error(Renum.ERROR.getCode(),"删除失败");
                }
            }else{
                return ResultUtil.error(Renum.DATA_IS_NULL.getCode(),"数据不能为空");
            }
        }catch (Exception e){
            return ResultUtil.error(Renum.UNKNOWN_ERROR.getCode(),Renum.UNKNOWN_ERROR.getMsg());
        }

    }
}
